//
//  ViewController.m
//  AppForInterview
//
//  Created by fangyuan ding on 2020/12/9.
//

#import "ViewController.h"
#import "Son.h"
#import "Sark.h"
#import <ReactiveObjC.h>
@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic, strong)UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self TestClass];
//    [self Invocation];
    [self initUI];
//    [self RAC];


}
- (void)initUI{
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 120, 50)];
    self.textField.delegate = self;
    [self.textField  setBackgroundColor:[UIColor systemGroupedBackgroundColor]];
       [self.textField  setCenter:self.view.center];
    [self.view addSubview:self.textField];
    [[self.textField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"%@",x);
        }];
    UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        NSLog(@"three:%@",x);
        }];
    [self.view addGestureRecognizer:tap];
    [[self.textField rac_valuesAndChangesForKeyPath:@"text" options:NSKeyValueObservingOptionNew observer:self] subscribeNext:^(RACTwoTuple<id,NSDictionary *> * _Nullable x) {
        NSLog(@"%@",x);
        }];
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"键盘弹起");
        }];
    [[self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"打印点击信息:%@",x);
        }];
    [[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"doNothing:%@",x);
        }];
    [[[self.textField rac_textSignal] filter:^BOOL(NSString * _Nullable value) {
            if(value.length == 3){
                return YES;
            }
            return NO;
        }] subscribeNext:^(NSString * _Nullable x) {
            NSLog(@"filter:%@",x);
        }];
    [[[self.textField rac_textSignal] map:^id _Nullable(NSString * _Nullable value) {
            if(value.length == 3){
                return @"map now";
            }
            return value;
        }] subscribeNext:^(id  _Nullable x) {
            NSLog(@"map:%@",x);
        }];
    RACSignal *firstCombineSignal = [self.textField rac_textSignal];
    RACSignal *secondCombineSignal = [tap rac_gestureSignal];
    RAC(self.textField,backgroundColor) = [RACSignal combineLatest:@[firstCombineSignal,secondCombineSignal] reduce:^id (NSString *text,UITapGestureRecognizer *tap){
        if(text.length == 3 &&tap.state == UIGestureRecognizerStateEnded){
            return [UIColor redColor];
        }
        return [UIColor cyanColor];
    }];
    //遍历数组
    NSArray *arr = @[@"1",@"2",@"3",@"4",@"5"];
    [arr.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"arr:%@",x);
    }];
    NSDictionary *dic = @{@"name":@"yangBo",@"age":@"19"};
    [dic.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"dic : %@",x);
    }];
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"开始编辑了");
    
}
//地址
- (void)TestClass{
//        Son *son = [[Son alloc]init];
//        NSLog(@"%@",son);
//
//        id cls = [Sark class];
//        void *obj = &cls;
//        [(__bridge id)obj speak];
//        my name's <ViewController: 0x7fcc14e08fe0>
//
//
//        NSLog(@"ViewController = %@,地址 = %p",self,&self);
////        ViewController = <ViewController: 0x7fa914c08a70>,地址 = 0x7ffee4c74048
//        id cls = [Sark class];
//        NSLog(@"Sark class = %@ 地址 = %p",cls,&cls);
//        //Sark class = Sark 地址 = 0x7ffee4c74028
//        void *obj = &cls;
//        NSLog(@"void *obj = %@ 地址 = %p",obj,&obj);
//        //void *obj = <Sark: 0x7ffee4c74028> 地址 = 0x7ffee4c74020
//        [(__bridge id)obj speak];
//        //my name's <ViewController: 0x7fa914c08a70>
//        Sark *sark = [[Sark alloc]init];
//        NSLog(@"Sark instance =%@ 地址 =%p",sark,&sark);
//        //Sark instance =<Sark: 0x6000036d05e0> 地址 =0x7ffee4c74018
//        [sark speak];
//         my name's (null)
      

        
        NSLog(@"ViewController = %@,地址 = %p",self,&self);
        //ViewController = <ViewController: 0x7fd9d4609030>,地址 = 0x7ffee153a048
        NSString *myName = @"halfrost";
        id cls = [Sark class];
        NSLog(@"Sark class = %@ 地址 = %p",cls,&cls);
        //Sark class = Sark 地址 = 0x7ffee153a020
        void *obj = &cls;
        NSLog(@"Void *obj = %@ 地址 = %p",obj,&obj);
        //Void *obj = <Sark: 0x7ffee153a020> 地址 = 0x7ffee153a018
        [(__bridge id)obj speak];
        //my name's halfrost
        Sark *sark = [[Sark alloc]init];
        //Sark instance = <Sark: 0x600000cf0550> 地址 = 0x7ffee153a010
        NSLog(@"Sark instance = %@ 地址 = %p",sark,&sark);
        [sark speak];
//        /my name's (null)
}
//将方法调用封装为对象的设计模式(命令模式)
- (void)Invocation{
    NSMethodSignature *signature = [ViewController instanceMethodSignatureForSelector :@selector(sendMessageWithPhone :WithName: )] ;
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation. target = self; //目标:接收消息的对象
    invocation. selector = @selector( sendMessageWithPhone :WithName:); //选择器:被发送的消息，方法必须和签名中的方法一致。
    NSString *phone = @"13512345678";
    //注意:设置参数的索引时不能从0开始，因为0已经被self占用，1已经被_ .cmd占用
    [invocation setArgument :&phone atIndex:2]; //参数:可以添加任意数量的参数。
    NSString *name = @"Dezi";
    [invocation setArgument :&name atIndex:3];
    /*
    注:调用invocation的invoke方法，就代表需要执行NSInvocation对象中指定对象的指定方法，并且传递指定的参数
    */
    [invocation invoke] ;
  
    
}
-(void)sendMessageWithPhone :(NSString* )phone WithName:(NSString*)name {
NSLog(@"电话号=%@，姓名=%@",phone, name);
//电话号=13512345678， 姓名Dezi
}
- (void)RAC{
    //创建  此时为冷信号 并不会被触发
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        //发送信号
        [subscriber sendNext:@"oh my god"];
        //回收资源。注意：手动创建一个signal一定要记得回收资源，不然程序会奔溃
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"信号发送完成");
        }];
    }];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"signalContent:%@",x);
    }];
}
@end
